package cn.changein.framework.service.setting.hotNotice.impl;


import cn.changein.framework.model.setting.hotNotice.entity.HotNotice;
import cn.changein.framework.model.setting.hotNotice.mapper.HotNoticeMapper;
import cn.changein.framework.model.setting.hotNotice.model.params.HotNoticeParam;
import cn.changein.framework.model.setting.hotNotice.model.result.HotNoticeResult;
import cn.changein.framework.service.setting.hotNotice.HotNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.changein.framework.util.ToolUtil;
import java.util.List;
import cn.changein.framework.base.pojo.page.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.changein.framework.base.pojo.page.PageFactory;
import java.io.Serializable;

/**
 * <p>
 * 公告 服务实现类
 * </p>
 *

 * @Date 2020-12-22
 * @Copyright: companyName
 * @Version 1.0
 * @Since JDK1.8
 */
@Service
public class HotNoticeServiceImpl extends ServiceImpl<HotNoticeMapper, HotNotice> implements HotNoticeService {

    @Override
    public void add(HotNoticeParam param){
        HotNotice entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(HotNoticeParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(HotNoticeParam param){
        HotNotice oldEntity = getOldEntity(param);
        HotNotice newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public HotNoticeResult findBySpec(HotNoticeParam param){
        return null;
    }

    @Override
    public List<HotNoticeResult> findListBySpec(HotNoticeParam param){

        List<HotNoticeResult> list = this.baseMapper.customList(param);
        if(list!=null && list.size()>0){
            return list;
        }
        return null;
    }

    @Override
    public PageInfo findPageBySpec(HotNoticeParam param){
        //Page pageContext = getPageContext();
        Page pageContext = param.getPage() != null || param.getLimit() != null ? new Page(param.getCorrectPage(), param.getCorrectLimit()) : getPageContext();

        IPage page = this.baseMapper.customPageList(pageContext, param);
        return PageFactory.createPageInfo(page);
    }

    private Serializable getKey(HotNoticeParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

    private HotNotice getOldEntity(HotNoticeParam param) {
        return this.getById(getKey(param));
    }

    private HotNotice getEntity(HotNoticeParam param) {
        HotNotice entity = new HotNotice();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
